package main

import (
	"context"
	"os"
	"os/signal"
	"syscall"

	"gitee.com/rw555/fdsmastergo/internal/cloud"
	"gitee.com/rw555/fdsmastergo/pkg/logger"
	"gitee.com/rw555/fdsmastergo/pkg/version"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	// 初始化日志
	logger.Configure(logger.Config{
		EnableConsole:       true,
		EnableCaller:        true,
		LogDirByServiceName: "cloud_server",
		MaxSize:             1,
		MaxBackups:          3,
		MaxAge:              3,
		SystemLogLevel:      zapcore.InfoLevel,
		OperationLogLevel:   zapcore.InfoLevel,
		NetworkLogLevel:     zapcore.InfoLevel,
		OtherLogLevel:       zapcore.DebugLevel,
	})
	defer logger.Sync()

	// 输出版本信息
	vInfo := version.NewInfo("cloud_server")
	logger.SystemInfo("云服务启动",
		zap.String("version", vInfo.Version),
		zap.String("gitCommit", vInfo.GitCommit),
		zap.String("buildTime", vInfo.BuildTime),
		zap.String("platform", vInfo.Platform))

	// 创建云服务
	service, err := cloud.NewCloudService()
	if err != nil {
		logger.SystemError("创建云服务失败", zap.Error(err))
	}

	// 创建上下文，用于优雅关闭
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// 启动云服务
	if err := service.Start(ctx); err != nil {
		logger.SystemError("启动云服务失败", zap.Error(err))
	}

	// 等待中断信号
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
	<-sigChan

	// 优雅关闭
	service.Stop()
}
